package exttable.testfiller;

import exttable.dbclasses.DbFile;
import exttable.formgenerator.StaticMethods;
import exttable.testfiller.formcomponents.EInputComponents;
import exttable.testselector.VolunteerSelectorController;
import exttable.userdefineextendsclasses.userDefineJFileChooser.UserDefineJFileChooserWithView;
import exttable.view.ViewInputFormComponents;
import exttable.view.ViewTestComboBox;
import utils.Localisation;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Jan 29, 2010
 * Time: 11:46:57 AM
 * To change this template use File | Settings | File Templates.
 */
class ValueRetriver {
    public static final String NO_DATA = "-1";
    public static final Map<Class, EInputComponents> mapCLasses = new HashMap<Class, EInputComponents>();
    boolean exccedLimits = false;

    public Vector<Vector> MapGenerator(List<ViewInputFormComponents> components) {

        Vector<Vector> toReturn = new Vector<Vector>();

        for (ViewInputFormComponents res : components) {

            switch (mapCLasses.get(res.getComponent().getClass())) {
                case INPUT_TEXT:
                   toReturn.add(addRowValue((JTextField) res.getComponent(), res));
                    break;
                case RADIO_BUTTON:
                    toReturn.add(addRowValue((JPanel) res.getComponent(), res));
                    break;
                case SELECT_ONE_MENU:
                    toReturn.add(addRowValue((JComboBox) res.getComponent(), res));
                    break;
                case TEXT_AREA:
                    toReturn.add(addRowValue((JTextArea) res.getComponent(), res));
                    break;
                case SKIP_ELEMENT:
                    break;
                case FILE:
                    toReturn.add(addRowValue((UserDefineJFileChooserWithView) res.getComponent(), res));
                    break;
                default: {
                    throw new IllegalArgumentException("no such element");
                }
            }
        }
        return toReturn;
    }

    private Vector<String> addRowValue(JTextField jf, ViewInputFormComponents res) {
        Vector<String> row = new Vector<String>();
        row.add(res.getAnalysis().toString());
        row.add(!jf.isEnabled() ? Localisation.getMessage("form.noinfo") : jf.getForeground().equals(Color.RED) ? "<html><font color=red>" + jf.getText() + "</font></html>" : jf.getText());
        row.add(jf.getName());
        row.add(!jf.isEnabled() ? NO_DATA : jf.getText());
        if (jf.getForeground().equals(Color.RED)) {
            exccedLimits = true;
        }
        return row;
    }

    private Vector<String> addRowValue(JComboBox jc, ViewInputFormComponents res) {
        Vector<String> row = new Vector<String>();
        ViewTestComboBox ttcc = (ViewTestComboBox) jc.getSelectedItem();
        row.add(res.getAnalysis().toString());
        row.add(!jc.isEnabled() ? Localisation.getMessage("form.noinfo") : ttcc.getNameRu());
        row.add(jc.getName());
        row.add(!jc.isEnabled() ? NO_DATA : String.valueOf(ttcc.getId()));

        return row;
    }

    private Vector<String> addRowValue(JPanel panel, ViewInputFormComponents res) {
        Vector<String> row = new Vector<String>();
        row.add(res.getAnalysis().toString());

        JRadioButton jb1 = (JRadioButton) panel.getComponent(0);
        JRadioButton jb2 = (JRadioButton) panel.getComponent(1);
        if (panel.isEnabled()) {
            if (jb1.isSelected()) {
                row.add(Localisation.getMessage("form.yes"));
                row.add(panel.getName());
                row.add("1");
            }
            if (jb2.isSelected()) {
                row.add(Localisation.getMessage("form.no"));
                row.add(panel.getName());
                row.add("0");
            }
        } else {
            row.add(Localisation.getMessage("form.noinfo"));
            row.add(panel.getName());
            row.add(NO_DATA);
        }
        return row;
    }

    private Vector<String> addRowValue(JTextArea jta, ViewInputFormComponents res) {
        Vector<String> row = new Vector<String>();
        row.add(res.getAnalysis().toString());
        row.add(!jta.isEnabled() ? Localisation.getMessage("form.noinfo") : jta.getText());
        row.add(jta.getName());
        row.add(!jta.isEnabled() ? NO_DATA : jta.getText());
        return row;

    }

    private Vector<String> addRowValue(UserDefineJFileChooserWithView userDefineJFileChooser, ViewInputFormComponents res) {
        String fileName=VolunteerSelectorController.viewProbationer.getProbationCode()
                +"_"+InputFormDisplay.getViewAnalysis().getAnalysisName()+
                "_"+StaticMethods.getTestTime(InputFormDisplay.getGroupSchId(), InputFormDisplay.getVisit());
        Vector<String> row = new Vector<String>();
        row.add(res.getAnalysis().toString());
//        row.add(!jButton.isEnabled() ? Localisation.getMessage("form.noinfo") : "-1");
//        row.add(jButton.getName());
//        row.add(!jButton.isEnabled() ? NO_DATA : jButton.getText());
        row.add(userDefineJFileChooser.getSelectedFile()==null ? "no file" : fileName);
        row.add(userDefineJFileChooser.getName());
        row.add(userDefineJFileChooser.getSelectedFile()==null ? "-1" : StaticMethods.getFileId(fileName).toString());
        System.out.println("groupSchId : "+InputFormDisplay.getGroupSchId());
        System.out.println("visit : "+ StaticMethods.getTestTime(InputFormDisplay.getGroupSchId(), InputFormDisplay.getVisit()));
        if(userDefineJFileChooser.getSelectedFile()==null){
            InputFormDisplay.selectedFile=null;
            InputFormDisplay.dbFile=null;
        }else{
            InputFormDisplay.selectedFile=userDefineJFileChooser.getSelectedFile();
            InputFormDisplay.dbFile=new DbFile();
            InputFormDisplay.dbFile.setFileId(StaticMethods.getFileId(fileName));
            InputFormDisplay.dbFile.setFileName(fileName);
            InputFormDisplay.dbFile.setFileEhtension(userDefineJFileChooser.getSelectedFile().getName().split("\\.")[1]);
            InputFormDisplay.dbFile.setSourceFileName(userDefineJFileChooser.getSelectedFile().getName());
            InputFormDisplay.dbFile.setTableName(InputFormDisplay.getViewAnalysis().getAnalysisName());
            InputFormDisplay.dbFile.setSumCRC32(UserDefineJFileChooserWithView.calculateSumCRC32(userDefineJFileChooser.getSelectedFile()));
        }
        return row;

    }

    static {
        mapCLasses.put(JTextField.class, EInputComponents.INPUT_TEXT);
        mapCLasses.put(JComboBox.class, EInputComponents.SELECT_ONE_MENU);
        mapCLasses.put(JPanel.class, EInputComponents.RADIO_BUTTON);
        mapCLasses.put(JTextArea.class, EInputComponents.TEXT_AREA);
        mapCLasses.put(JLabel.class, EInputComponents.SKIP_ELEMENT);
        mapCLasses.put(JCheckBox.class, EInputComponents.SKIP_ELEMENT);
        mapCLasses.put(UserDefineJFileChooserWithView.class, EInputComponents.FILE);
    }

}
